package com.guofen.service.shlj.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.ShljDeodorantSprayingtimeMapper;
import com.guofen.pojo.ShljAbnormalGarbage;
import com.guofen.pojo.ShljBottleCorrodibleSortingStatistic;
import com.guofen.pojo.ShljDeodorantSprayingtime;
import com.guofen.service.shlj.DeodorantSprayingtimeService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DeodorantSprayingtimeServiceImpl implements DeodorantSprayingtimeService {
    @Autowired
    private ShljDeodorantSprayingtimeMapper shljDeodorantSprayingtimeMapper;

    @Override
    public boolean addForm(String time,String signature) {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();
        //获取当天位星期几
        Date weekNow = new Date();
        SimpleDateFormat dateFm = new SimpleDateFormat("EEEE", Locale.CHINA);
        String week = dateFm.format(weekNow);
        System.out.println(week);

        ShljDeodorantSprayingtime shljDeodorantSprayingtime = new ShljDeodorantSprayingtime();
        shljDeodorantSprayingtime.setDate(date);
        shljDeodorantSprayingtime.setWeek(week);
        shljDeodorantSprayingtime.setTime(time);
        shljDeodorantSprayingtime.setSignature(signature);
        int result = shljDeodorantSprayingtimeMapper.insert(shljDeodorantSprayingtime);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<ShljDeodorantSprayingtime> list = shljDeodorantSprayingtimeMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<ShljDeodorantSprayingtime> list = shljDeodorantSprayingtimeMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return shljDeodorantSprayingtimeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(ShljDeodorantSprayingtime record) {
        return shljDeodorantSprayingtimeMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<ShljDeodorantSprayingtime> findForm() {
        return shljDeodorantSprayingtimeMapper.findLast();
    }
}
